package farmer.gui;

import farmer.game.Data;
import farmer.game.MainGameState;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import farmer.update.building.Farm;
import farmer.update.building.LumberCamp;
import farmer.update.building.Mine;
import farmer.update.building.Road;
import farmer.update.building.Windmill;
import farmer.update.doodad.Tree;

import farmer.core.Drawable;
import core.gui.GlassPane;

public class BuildingGUI implements Drawable{
	
	public static int cityNum = 0;
	public static int farmNum = 1;
	public static int windmillNum = 2;
	public static int lumberjackNum = 3;
	public static int mineNum = 4;
	public static int houseNum = 5;
	public static int roadNum = 6;
	
	int selectionType = -1;
	
	private ArrayList<BuildingButton> buttonList = new ArrayList<BuildingButton>();
	
	GlassPane glassPane;
	BuildingButton city = new BuildingButton("City", cityNum, this);
	BuildingButton farm = new BuildingButton("Farm", farmNum, this);
	BuildingButton windmill = new BuildingButton("Mill", windmillNum, this);
	BuildingButton lumberjack = new BuildingButton("Camp", lumberjackNum, this);
	BuildingButton mine = new BuildingButton("Mine", mineNum, this);
	BuildingButton house = new BuildingButton("House", houseNum, this);
	BuildingButton road = new BuildingButton("Road", roadNum, this);
	BuildingButton tree = new BuildingButton("Tree", Data.tree, this);
	BuildingButton destroy = new BuildingButton("Destroy", Data.destroy, this);
	//Menu button
	MainGameMenuButton menu;
	
	public BuildingGUI(MainGameState state){
		//Initialize GlassPane
		glassPane = state.getGlassPane();
		//Add button to the GlassPane
		//glassPane.addComponent(city);
		buttonList.add(city);
		city.setPosition(25, 10);
		glassPane.addComponent(farm);
		buttonList.add(farm);
		farm.setPosition(10, 70-40);
		glassPane.addComponent(windmill);
		buttonList.add(windmill);
		windmill.setPosition(10, 130-40);
		glassPane.addComponent(lumberjack);
		buttonList.add(lumberjack);
		lumberjack.setPosition(10, 190-40);
		glassPane.addComponent(mine);
		buttonList.add(mine);
		mine.setPosition(10, 250-40);
		//glassPane.addComponent(house);
		//house.setPosition(10, 310);
		glassPane.addComponent(road);
		buttonList.add(road);
		road.setPosition(10, 370-60-25);
		glassPane.addComponent(tree);
		buttonList.add(tree);
		tree.setPosition(10, 430-60-25);
		glassPane.addComponent(destroy);
		buttonList.add(destroy);
		destroy.setPosition(30, 490-30-20);
		//Menu button
		menu = new MainGameMenuButton("Menu", state);
		glassPane.addComponent(menu);
		menu.setPosition(20, 540);
		menu.setSize(70, 40);
	}
	
	public void update(){
		for(BuildingButton button : buttonList){
			button.calculateRessource();
		}
	}
	
	public int getKeyCode(int num){
		if(num == cityNum){
			return KeyEvent.VK_0;
		}
		if(num == farmNum){
			return KeyEvent.VK_F;
		}
		if(num == windmillNum){
			return KeyEvent.VK_M;
		}
		if(num == lumberjackNum){
			return KeyEvent.VK_C;
		}
		if(num == mineNum){
			return KeyEvent.VK_N;
		}
		if(num == houseNum){
			return KeyEvent.VK_H;
		}
		if(num == roadNum){
			return KeyEvent.VK_R;
		}
		if(num == Data.tree){
			return KeyEvent.VK_T;
		}
		if(num == Data.destroy){
			return KeyEvent.VK_X;
		}
		return -1;
	}

	public void setSelection(int num){
		Data.building = num;
		selectionType = num;
		city.setSelected(false);
		farm.setSelected(false);
		windmill.setSelected(false);
		lumberjack.setSelected(false);
		mine.setSelected(false);
		house.setSelected(false);
		road.setSelected(false);
		tree.setSelected(false);
		destroy.setSelected(false);
		if(num == cityNum){
			city.setSelected(true);
		}
		if(num == farmNum){
			farm.setSelected(true);
		}
		if(num == windmillNum){
			windmill.setSelected(true);
		}
		if(num == lumberjackNum){
			lumberjack.setSelected(true);
		}
		if(num == mineNum){
			mine.setSelected(true);
		}
		if(num == houseNum){
			house.setSelected(true);
		}
		if(num == roadNum){
			road.setSelected(true);
		}
		if(num == Data.tree){
			tree.setSelected(true);
		}
		if(num == Data.destroy){
			destroy.setSelected(true);
		}
	}
	
	Color panelColor = new Color(67, 181, 241);
	Color panelColor2 = new Color(237, 221, 97);
	Color panelColor3 = new Color(80, 122, 12);
	Color panelColor4 = new Color(188, 187, 143);
	Color panelColor5 = new Color(189, 217, 182);
	
	Color woodColor = new Color(165, 92, 12);
	Color rockColor = Color.gray;
	
	BasicStroke stroke = new BasicStroke(5);
	
	private Font font = new Font("Button", Font.BOLD, 13);

	@Override
	public void draw(Graphics2D g) {
		g.setColor(panelColor4);
		g.fillRect(0, 0, 110, 600);
		g.setColor(Color.black);
		g.setFont(new Font("Title", Font.ITALIC, 15));
		g.drawString("Building menu", 8, 20);
		g.setColor(Color.black);
		g.setStroke(stroke);
		g.drawRect(2, 2, 108, 596);
		g.drawLine(110, 0, 110, 600);
		g.setStroke(new BasicStroke(1.5f));
		g.drawLine(0, 272, 110, 272);
		g.drawLine(0, 407, 110, 407);
		//glassPane.draw(g);
		
		//Draw the cost
		g.setFont(font);
		g.setColor(Color.black);
		int posX = 0;
		int posY = 0;
		int wood = 0;
		int rock = 0;
		//Farm
		wood = Farm.woodCostS;
		rock = Farm.rockCostS;
		posX = 65;
		posY = 52;
		g.setColor(woodColor);
		g.drawString("W : "+wood, posX, posY);
		g.setColor(rockColor);
		g.drawString("R  : "+rock, posX, posY+15);
		//Mill
		wood = Windmill.woodCostS;
		rock = Windmill.rockCostS;
		posX = 65;
		posY += 60;
		g.setColor(woodColor);
		g.drawString("W : "+wood, posX, posY);
		g.setColor(rockColor);
		g.drawString("R  : "+rock, posX, posY+15);
		//LumberCamp
		wood = LumberCamp.woodCostS;
		rock = LumberCamp.rockCostS;
		posX = 65;
		posY += 60;
		g.setColor(woodColor);
		g.drawString("W : "+wood, posX, posY);
		g.setColor(rockColor);
		g.drawString("R  : "+rock, posX, posY+15);
		//Mine
		wood = Mine.woodCostS;
		rock = Mine.rockCostS;
		posX = 65;
		posY += 60;
		g.setColor(woodColor);
		g.drawString("W : "+wood, posX, posY);
		g.setColor(rockColor);
		g.drawString("R  : "+rock, posX, posY+15);
		//Road
		wood = Road.woodCostS;
		rock = Road.rockCostS;
		posX = 65;
		posY += 100;
		g.setColor(woodColor);
		g.drawString("W : "+wood, posX, posY-25);
		g.setColor(rockColor);
		g.drawString("R  : "+rock, posX, posY+15-25);
		//Tree
		wood = Tree.woodCost;
		rock = Tree.rockCost;
		posX = 65;
		posY += 60;
		g.setColor(woodColor);
		g.drawString("W : "+wood, posX, posY-25);
		g.setColor(rockColor);
		g.drawString("R  : "+rock, posX, posY+15-25);
		
		//Draw the line at the bottom + menu button
		g.setStroke(new BasicStroke(3));
		g.setColor(Color.black);
		g.drawLine(0, 520, 110, 520);
	}

}
